Learn R Programming

searchable (version 0.3.3.1)

[,Searchable,PatternOrCharacter,missing-method: Extraction operators for Searchable object

Description

Defines [, [[, and $ for Searchable objects

Usage

## S3 method for class 'Searchable,PatternOrCharacter,missing':
[(x, i, j, ..., drop = TRUE)

## S3 method for class 'Searchable,character,missing': [(x, i) <- value

Arguments

x
Searchable object
i
character; pattern with potential match modifiers applied,
j
missing; never specified
...
additional arguments. See Extract
drop
For matrices and arrays. If TRUE the result is coerced to the lowest possible dimension (see the examples). This only works for extracting elements, not for the replacement. See drop for further details.
value
replacement value for replacement functions

The methods for searching respect the modifiers applied to both x and i.

Value

  • The values after the extracting methods have been applied: [ returns a subset of x, but which is not Searchable. [[ and $ return a sinlge element of x

<code>[</code>, <code>[&lt;-</code>

[ and [<- are used for subsetting and replacing zero or more elemenxts of x. Used with searchable objects, these operators differ from normal R operations in the following respects:

  • The search returns elements of the target that matchesANYof the search patterns.
  • Unlike the its normal behavior,[does not guarantee the output to have as many elements as elements topattern.
  • [does not return a Searchable object. It is thought that the return valuable will not be subsequently searched. It is easy to turn the results into a Searchable object usingsearchablehowever.
  • Unlike for environments and hashes, no constraints exist for ensuring uniqueness for names in vectors and lists. These structures may contain multiple elements with the same name. Normal attempts to extract by name yield only thefirstelement that matches the name. Using aSearchablepatterns match yields all matching elements.

See Also

Searchable Extract Match mofiers: fixed, regex, coll and ignore.case reverse.lookup

Examples

Run this code
# ATOMIC VECTORS:
    v <- c( a=1, b=2, B=3, c=4, c2=5 )
    sv <- searchable(v)


  # FLEXIBLY FIND ELEMENTS BY NAME
    sv[ regex('c') ]
    sv[ fixed('c') ]

    sv[ ignore.case('b') ]


  # FLEXIBLY REPLACEMENT ELEMENTS BY NAME
    sv[ regex('c.?') ]   <- "3rd"


  # SET DEFAULT SEARCH FOR TARGET/OBJECT
    sv <- searchable(v, case_insensitive = TRUE )
    sv['b']
    sv['B']

    sv <- regex(sv)
    sv['c']

    sv <- ignore.case(sv)
    sv['b']
    sv['c']                  # st


  # USE ON (RECURSIVE) LISTS:
    l <- list( a=1, b=2, c=3 )
    sl <- searchable(l)
    sl["b"]
    sl[ ignore.case("B") ]


  # USE WITH MAGRITTR
   sl[ "B"  %>% ignore.case ]
    "b" %>% sl[.]
    "B" %>% ignore.case %>% sl[.]

Run the code above in your browser using DataLab